<template>
	<view class="home">
		<!-- 搜索框区域 -->
		<my-search></my-search>
		<!-- 轮播图区域 -->
		<view class="home-swiper">
			<swiper class="swiper" indicator-dots :autoplay="true" circular interval=3000>
				<swiper-item v-for="item in swiperList" :key="item.goods_id">
					<navigator class="swiper-item" :url="`/subpage/details/details?goods_id=${item.goods_id}`"
						:open-type="item.open_type">
						<image :src="item.image_src"></image>
					</navigator>
				</swiper-item>
			</swiper>
      <!-- 滚动条 -->
      <uni-notice-bar @click="toggle('center')" speed="70" show-icon scrollable
      	text="下次记得从 微信底部 [发现] >>> [购物] 进入小程序,解锁更多福利 详情戳 >>> " />
		</view>
    <!-- 弹框 -->
    <uni-popup ref="popup" background-color="#fff" @change="change" :mask-click="false">
    	<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
        <text class="text">想得美</text>
        <button @click="close">x</button>
          </view>
    </uni-popup>
		<!-- 分类导航区域 -->
		<view class="home-nav">
			<view class="home-nav" v-for="(item,index) in navList" :key="index" @click="goToSort(item)">
				<image class="nav-item-icon" :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层商品区域 -->
		<view class="home-floor">
			<view class="home-floor-box" v-for="(item1,index1) in floorList" :key="index1">
				<view class="floor-box-top">
					<image :src="item1.floor_title.image_src"></image>
				</view>
				<view class="floor-box-bottom">
					<navigator class="floor-box-bottom-left" :url="item1.product_list[0].navigator_url"
						:open-type="item1.product_list[0].open_type">
						<image :src="item1.product_list[0].image_src"
							:style="{width:item1.product_list[0].image_width + 'rpx'}" :mode="widthFix"></image>
					</navigator>
					<view class="floor-box-bottom-right">
						<navigator class="bottom-right-item" v-for="(item2,index2) in item1.product_list" :key="index2"
							v-if="index2 !== 0" :url="item2.navigator_url" :open-type="item2.open_type">
							<image :src="item2.image_src"
								:style="{width:item2.image_width + 'rpx'}"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from '../../mixins/tabbarbage.js'
	export default {
		data() {
			return {
				// 轮播图数据
				swiperList: [],
				// 分类导航数据
				navList: [],
				// 楼层数据
				floorList: []
			};
		},
    onReady() {},
		mixins: [badgeMixin],
		methods: {
      // 弹框
      change(e) {
      	this.show = e.show
      },
      // 关闭弹窗
      close() {
      	this.$refs.popup.close()
      },
      toggle(type) {
      	this.type = type
      	// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
      	this.$refs.popup.open(type)
      },


			// 获取轮播图数据
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get("/api/public/v1/home/swiperdata");
				if (res.meta.status !== 200) return uni.$showToast();
				this.swiperList = res.message;
			},
			// 获取分类导航数据
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get("/api/public/v1/home/catitems");
				if (res.meta.status !== 200) return uni.$showToast();
				this.navList = res.message;
			},
			// 跳转到分类页
			goToSort(item) {
				if (item.name == "分类") {
					uni.switchTab({
						url: "/pages/sort/sort"
					})
				}
			},
			// 获取楼层数据
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get("/api/public/v1/home/floordata");
				if (res.meta.status !== 200) return uni.$showToast();
				this.rewriteFloorList(res.message);
				this.floorList = res.message;
			},
			// 对楼层数据进行重写
			rewriteFloorList(data) {
				data.forEach((item) => {
					item['product_list'].forEach((item) => {
						item['navigator_url'] = item['navigator_url'].replace(/\/pages\/goods_list/g,
							"/subpage/lists/lists");
					})
				})
			}
		},
		onLoad() {
			// 获取轮播图数据
			this.getSwiperList();
			// 获取分类导航数据
			this.getNavList();
			// 获取楼层数据
			this.getFloorList();
		}
	}
</script>

<style lang="scss">
	.home {
		// 轮播图区域
		.home-swiper {
			.swiper {
				width: 100%;
				height: 330rpx;

				.swiper-item {
					width: 100%;
					height: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
      // 滚动条
      .uni-noticebar {
        padding-top: 0;
        padding-bottom: 0;
        padding-right: 0;
        margin: 0;
        .uni-noticebar__content-text {
           font-size: 12px;
        }
      }
		}
    // 弹框
    	@mixin flex {
    		/* #ifndef APP-NVUE */
    		display: flex;
    		/* #endif */
    		flex-direction: row;
    	}
    
    	@mixin height {
    		/* #ifndef APP-NVUE */
    		height: 100%;
    		/* #endif */
    		/* #ifdef APP-NVUE */
    		flex: 1;
    		/* #endif */
    	}
    	.popup-content {
    		@include flex;
    		align-items: center;
    		justify-content: center;
    		padding: 15px;
    		height: 50px;
    		background-color: #fff;
        .text {
        	font-size: 12px;
        	color: #333;
        }
    	}
    
    	.popup-height {
    		@include height;
    		width: 200px;
    	}
    
		// 分类导航区域
		.home-nav {
			display: flex;
			justify-content: space-around;

			.home-nav {
				width: 33.33%;
				margin: 15px 0;

				.nav-item-icon {
					width: 120rpx;
					height: 130rpx;
				}
			}
		}

		// 楼层商品区域
		.home-floor {
			padding: 0 5px;

			.home-floor-box {
				margin-bottom: 5px;

				.floor-box-top {
					image {
						width: 100%;
						height: 60rpx;
						display: block;
					}
				}

				.floor-box-bottom {
					display: flex;
					width: 100%;

					.floor-box-bottom-left {
						margin-right: 5px;

						image {
							display: block;
						}
					}

					.floor-box-bottom-right {
						display: flex;
						flex-wrap: wrap;
						justify-content: space-around;
						align-content: space-between;

						.bottom-right-item {
							image {
								display: block;
								height: 235rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
